﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    public class ManagePendingRequest
    {
        /// <summary>
        ///Metoda priradi pozadavek k prihlaseni do tymu.
        ///</summary>
        public static void ApplyUserToTeam(int UserID, int TeamID)
        {
            AppLib.PendingRequest join = new AppLib.PendingRequest();
            var db = MidApp.LinqUse.DBConn;

            join.UserID = UserID;
            join.TeamID = TeamID;

            db.PendingRequests.InsertOnSubmit(join);
            db.SubmitChanges();
        }
        /// <summary>
        ///Metoda zjistuje, zda hrac s danym Id je cekatelem pro prijeti do tymu.
        ///</summary>
        public static bool IsTeamApplicant(int TeamID, int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            var files = (from PendingRequest in db.PendingRequests where PendingRequest.UserID == UserID && PendingRequest.TeamID == TeamID select PendingRequest).ToList();

            bool b = true;

            if (files.Count > 0)
                b = false;
            else
                b = true;

            return b;

        }
        /// <summary>
        ///Metoda vrati vsechny uzviatele cekajici na pripojeni k tymu.
        ///</summary>
        public static List<AppLib.PendingRequest> GetAllApplicants(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from PendingRequest in db.PendingRequests where PendingRequest.TeamID==TeamID select PendingRequest).ToList();
        }
        /// <summary>
        ///Metoda vymaze spojeni v tabulce.
        ///</summary>
        public static void DeleteRecord(int TeamID, int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            AppLib.PendingRequest resign = (from PendingRequest in db.PendingRequests where PendingRequest.UserID == UserID && PendingRequest.TeamID == TeamID select PendingRequest).First();

            db.PendingRequests.DeleteOnSubmit(resign);
            db.SubmitChanges();
        }
    }
}
